package com.xiangxiao.rpan.settings.controller;

import com.xiangxiao.rpan.settings.constant.ResponseCode;
import com.xiangxiao.rpan.settings.constant.ResponseMessage;
import com.xiangxiao.rpan.settings.dto.JsonObject;
import com.xiangxiao.rpan.settings.dto.SystemConfigDTO;
import com.xiangxiao.rpan.settings.entity.UpdateViewSetting;
import com.xiangxiao.rpan.settings.exception.SettingsException;
import com.xiangxiao.rpan.settings.service.SystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/11/23 17:24
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class SettingController {
  private static Logger logger = LoggerFactory.getLogger(SettingController.class);
  @Resource
  private SystemConfigService systemConfigService;

  @PutMapping("/config/view")
  public JsonObject updateViewSetting(@Valid @RequestBody UpdateViewSetting settingRequest) {
    try {
      SystemConfigDTO systemConfigDTO = new SystemConfigDTO();
      BeanUtils.copyProperties(settingRequest, systemConfigDTO);
      systemConfigService.updateSystemConfig(systemConfigDTO);
      return new JsonObject(ResponseMessage.MODIFY_DISPLAY_SETTING_SUECCESS, ResponseCode.OK, true);
    } catch (SettingsException e) {
      logger.error(ResponseMessage.MODIFY_DISPLAY_SETTING_FAILED, e);
      return new JsonObject(ResponseMessage.MODIFY_DISPLAY_SETTING_FAILED, ResponseCode.MODIFY_DISPLAY_SETTING_FAILED);
    }
  }
}
